package com.spring.campus_express_delivery.service.serviceImpl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.spring.campus_express_delivery.dao.NoticesDao;
import com.spring.campus_express_delivery.entity.Notices;
import com.spring.campus_express_delivery.entity.Users;
import com.spring.campus_express_delivery.service.NoticesService;
import com.spring.campus_express_delivery.util.MyUtil;
import com.spring.campus_express_delivery.util.ResponseEntity;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class NoticesServiceImpl implements NoticesService {
    @Resource
    NoticesDao noticesDao;
    //添加公告
    @Override
    public void insertNotices(Notices notices) {
        noticesDao.insertNotices(notices);
    }
    //公告列表
    @Override
//    public List<Notices> selectAll(){
//        return noticesDao.selectAll();
//    }
    //单个公告查询
    public Notices selectId(String noticeNum){
        return noticesDao.selectId(noticeNum);
    }
    //修改公告
   public void updateId(Notices notices){
        noticesDao.updateId(notices);
    }
    //删除公告
    public void deleteId(String noticeNum){
        noticesDao.deleteId(noticeNum);
    }
    //Excel导入公告信息
    @Override
    public void importNoticesExcel(MultipartFile noticeFiles) throws IOException {
        Workbook workbook=null;
        String fileName=noticeFiles.getOriginalFilename();
        if(fileName.endsWith("xls")){
            workbook=new HSSFWorkbook(noticeFiles.getInputStream());
        }else if(fileName.endsWith("xlsx")){
            workbook=new XSSFWorkbook(noticeFiles.getInputStream());
        }else{
            throw new IOException("文件不是Excel类型");
        }
        Sheet sheet=workbook.getSheet("sheet1");
        int rows=sheet.getLastRowNum();
        if(rows==0){
            throw new IOException("没有数据导入失败");
        }
        for(int i=1;i<rows+1;i++){
            Row row=sheet.getRow(i);
            if(row!=null){
                Notices notices=new Notices();
                MyUtil myUtil=new MyUtil();
                String noticeNum=myUtil.getCellValue(row.getCell(0));
                String noticeTitle=myUtil.getCellValue(row.getCell(1));
                String noticeContent= myUtil.getCellValue(row.getCell(2));
                Notices notices1=noticesDao.selectId(noticeNum);
                if(notices1!=null){
                    ResponseEntity responseEntity;
                    responseEntity=ResponseEntity.forbidden();
                    responseEntity.putDataValue("msg","公告编号重复");
                }else {
                    notices.setNoticeNum(noticeNum);
                    notices.setNoticeTitle(noticeTitle);
                    notices.setNoticeContent(noticeContent);
                    noticesDao.insertNotices(notices);
                }
            }
        }
    }
    @Override
    public void exportNoticesExcel(HttpServletResponse response) throws Exception {

        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("Sheet1");
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle style = wb.createCellStyle();
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        HSSFCell cell = row.createCell(0);
        cell.setCellValue("公告编号");//设置他的内容
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(1);//创建第二列
        cell.setCellValue("公告标题");//设置他的内容懂
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(2);//创建第二列
        cell.setCellValue("公告内容");//设置他的内容懂
        cell.setCellStyle(style);//设置样式
        cell = row.createCell(3);//创建第二列
        cell.setCellValue("公告时间");//设置他的内容懂
        cell.setCellStyle(style);//设置样式

//        cell.setCellValue("分数");
//        cell.setCellStyle(style);


        List<Notices> list = noticesDao.selectAll();
        for (int i = 0; i < list.size() ; i++) {
            row = sheet.createRow(i + 1);
            Notices notices = list.get(i);
            row.createCell(0).setCellValue(notices.getNoticeNum());
            row.createCell(1).setCellValue(notices.getNoticeTitle());
            row.createCell(2).setCellValue(notices.getNoticeContent());
            row.createCell(3).setCellValue(notices.getNoticeTime());
        }
//            row.createCell(2).setCellValue(stu.getScore());
          //  cell = row.createCell(4);


            OutputStream output = response.getOutputStream();
            //response.reset();
            long filename = System.currentTimeMillis();
            SimpleDateFormat df = new SimpleDateFormat("yyMMddHHmmss");
            String fileName = df.format(new Date());
            response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
            response.setContentType("application/msexcel");
            //response.reset();
//            response.addHeader("Access-Control-Allow-Origin","*");
//            response.addHeader("Access-Control-Allow-Methods","GET,POST,PUT,DELETE");
//            response.addHeader("Access-Control-Allow-Headers","Content-Type");
            wb.write(output);


    }

    @Override
    public List<Notices> selectNotice(String keyWord) {
        return noticesDao.selectNotice(keyWord);
    }
//    公告列表
    @Override
    public PageInfo<Notices> selectAll(int pageNo, int pageSize) {

        PageHelper.startPage(pageNo,pageSize);
        List<Notices> allNotices = noticesDao.selectAll();
        PageInfo<Notices> pageInfo = new PageInfo<>(allNotices);
        return pageInfo;
    }
}
